﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        //没有用数据库
        /*public static Dictionary<string, string> users = new Dictionary<string, string>();


        private void Form1_Load(object sender, EventArgs e)
        {
            users.Add("admin", "admin");
        }*/

        public static Dictionary<string, string> users = new Dictionary<string, string>();
        //登录按钮
        private void button1_Click(object sender, EventArgs e)
        {
            var uid = textBox1.Text;
            var pwd = textBox2.Text;
            


            string UserName = textBox1.Text.Trim();
            string PassWord = textBox2.Text.Trim();

            var usr = users.Where(x => x.Key == UserName && x.Value == PassWord).FirstOrDefault();

            var hasUid = !string.IsNullOrEmpty(uid);
            var hasPwd = !string.IsNullOrEmpty(pwd);

            if (uid!=null && pwd!=null)
            {
                MessageBox.Show("登录成功","提示");
            }

            //连接数据库
            string connect = "server=.;database=Blogs;uid=hzh;pwd=hzh2000623;";
            SqlConnection sqlConnection = new SqlConnection(connect);
            sqlConnection.Open();

            string sqlOrder = string.Format("select * from Users where UserName='{0}'and PassWord='{1}'", UserName, PassWord);
            SqlCommand sqlCommand = new SqlCommand(sqlOrder, sqlConnection);


            var show = sqlCommand.ExecuteReader();
            if (show.Read())
            {
                MainForm show1 = new MainForm();
                show1.Show();
            }
            else
            {
                MessageBox.Show("账号或密码错误", "提示");
            }
            sqlConnection.Close();



            /*
            var usr = users.Where(x => x.Key == uid && x.Value == pwd).FirstOrDefault();

            if (usr.Key != null && usr.Value != null)
            {
                MessageBox.Show("登录成功");
                MainForm mainForm = new MainForm();
                this.Hide();
                mainForm.Show();
            }
            else
            {
                MessageBox.Show("登录失败");
            }*/
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RegisterForm registerForm = new RegisterForm();
            registerForm.Show();
        }

    }
}
